package com.apes.fn.scm.rebatePolicy.repository;

import com.apes.fn.scm.rebatePolicy.model.RebatePolicy;
import com.apes.framework.jpa.repository.CustomRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;

public interface RebatePolicyRepository extends CustomRepository<RebatePolicy, String> {

    @Query(value = "SELECT A.SUPPLIER_ID SUPPLIER_ID,A.PLATFORM_COMPANY_ID PLATFORM_COMPANY_ID,A.COOPERATION_ID COOPERATION_ID,A.PARTNER_ID PARTNER_ID,A.ID FROM " +
            "(SELECT A.SUPPLIER_ID,A.PLATFORM_COMPANY_ID,A.COOPERATION_ID,A.ID," +
            "(CASE WHEN (SELECT T.CONTRACT_TYPE FROM ACC_CONTRACT T WHERE A.CONTRACT_ID = T.ID) = 'Contract.contractType.platform' THEN B.PARTNER_ID ELSE '' END) PARTNER_ID " +
            "FROM FN_REBATE_POLICY A LEFT JOIN FN_CONTRACT B ON A.PLATFORM_COMPANY_ID = B.COMPANY_ID AND B.STATE = 'approve' " +
            "WHERE A.STATE = 'approve' AND A.END_DATE >= to_date(:endDate,'yyyy-MM-dd') AND A.PERIOD_TYPE = :periodTypeId) A " +
            "GROUP BY A.SUPPLIER_ID,A.PLATFORM_COMPANY_ID,A.COOPERATION_ID,A.PARTNER_ID,A.ID",nativeQuery = true)
    List<Map> findRebatePolicy(@Param("endDate") String endDate, @Param("periodTypeId") String periodTypeId);

}
